﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TelephoneYiShou.XQL
{
    public partial class FrmShoppingPlanAdd : Skin_Mac
    {
        DataGridView dv;
        public FrmShoppingPlanAdd(DataGridView dvs)
        {
            InitializeComponent();
            dv = dvs;
        }

        //添加
        private void buttadd_Click(object sender, EventArgs e)
        {
            
            string num = textnum.Text;
            string jtime = textjtime.Text;
            string beuzhu = textbeizhu.Text;
            string shifo = comboBox1.Text;
            if (string.IsNullOrEmpty(textcommid.Text) || string.IsNullOrEmpty(txtcolor.Text) || string.IsNullOrEmpty(textgsid.Text)  || string.IsNullOrEmpty(num) || string.IsNullOrEmpty(jtime) || string.IsNullOrEmpty(beuzhu) || string.IsNullOrEmpty(shifo))
            {
                MessageBoxEx.Show("请把数据填写完整");
                return;
            }
            string sqlnames = string.Format("select commodityid from commodity where commodityname='{0}' and colour = '{1}'", textcommid.Text, txtcolor.Text);
            string sqlgyss = string.Format("select supplierid from supplier where suppliername = '{0}'", textgsid.Text);
            string commid = DBHelper.GetDataTable(sqlnames).Rows[0][0].ToString();
            string gsid = DBHelper.GetDataTable(sqlgyss).Rows[0][0].ToString();
            string isql = string.Format("insert into purchaseplan values ('{0}','{1}','{2}','{3}','{4}','{5}') ", commid, gsid, num, jtime, beuzhu, shifo);
            if (DBHelper.ExecuteNonQuery(isql))
            {
                MessageBoxEx.Show("添加成功");
                //更新数据
                string sql = "select purchaseplanid '进货计划编号',c.CommodityName '商品名称',s.SupplierName '供应商',p.Getnumber '进货数量',p.OrderTime '最晚进货时间',p.Remark '备注',p.complete '是否完成',c.Colour '颜色'from purchaseplan p,Commodity c, Supplier s where p.CommodityID = c.CommodityID and p.SupplierID = s.SupplierID";
                DataTable dt = DBHelper.GetDataTable(sql);
                dv.DataSource = dt;
            }
            else
            {
                MessageBoxEx.Show("添加失败");
            }
        }
        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            textcommid.Text = null;
            txtcolor.Text = null;
            textnum.Text = null;
            textjtime.Text = null;
            textbeizhu.Text = null;
        }
        //退出页面
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmShoppingPlanAdd_Load(object sender, EventArgs e)
        {
            //商品名称绑定
            string sqlname = "select distinct commodityname from commodity";
            DataTable dte = DBHelper.GetDataTable(sqlname);
            for (int i = 0; i < dte.Rows.Count; i++)
            {
                textcommid.Items.Add(dte.Rows[i][0]);
            }
            //供应商绑定
            string sqlgys = "select suppliername from supplier";
            dte = DBHelper.GetDataTable(sqlgys);
            for (int i = 0; i < dte.Rows.Count; i++)
            {
                textgsid.Items.Add(dte.Rows[i][0]);
            }
        }
        //颜色
        private void textgsid_MouseEnter(object sender, EventArgs e)
        {
            //颜色数据绑定
            txtcolor.Items.Clear();
            string sqlcolor = string.Format("select colour from commodity where commodityname = '{0}'", textcommid.Text);
            DataTable dtes = DBHelper.GetDataTable(sqlcolor);
            for (int i = 0; i < dtes.Rows.Count; i++)
            {
                txtcolor.Items.Add(dtes.Rows[i][0]);
            }
        }
    }
}
